package com.wentao.mall.entity

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import javax.persistence.AttributeConverter
import javax.persistence.Converter

@Converter
class SetConverter : AttributeConverter<Set<Any>, String> {
    private val mapper = ObjectMapper()

    override fun convertToDatabaseColumn(list: Set<Any>): String {
        return mapper.writeValueAsString(list)
    }

    override fun convertToEntityAttribute(string: String?): Set<Any> {
        return mapper.readValue<Set<Any>>(string ?: "[]")
    }
}